package com.ksp.wke.kspr01;
import static com.ksp.com.KSPCOM_BasConst.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksp.api.KSPAPI_UsrAuth;
import com.ksp.dom.KSPDOM_ResDta;
import com.ksp.dom.KSPDOM_TabDta;
import com.ksp.dom.KSPDOM_User;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONValue;

/**
 * 5.7.1KSPR01 新增回复
 * @author anye
 *
 */
@Controller
public class KSPR01Controller {
	
	private static ConcurrentLinkedQueue<Map<String, Object>> repQue = new ConcurrentLinkedQueue<>();

	private ExecutorService repService = Executors.newCachedThreadPool();

	@RequestMapping("/rep/KSPR01")
	@ResponseBody
	public KSPDOM_ResDta iniPam(ServletRequest request) {
		String wCooTkn = request.getParameter(COO_TKN);
		String wCmtTch = request.getParameter(CMT_TCH);
		String wTooOam = request.getParameter(TOO_OAM);
		String wTooUsr = request.getParameter(TOO_USR);
		String wRepCon = request.getParameter(REP_CON);
		
		if (!KSPAPI_UsrAuth.checkUsr(request, wCooTkn)) {
			return KSPDOM_ResDta.buildFail("请登录后再评论");
		}
		KSPDOM_User wCurUsr = KSPAPI_UsrAuth.getUsrInf(request);

		//
		Map<String, Object> wPamMap = new HashMap<>();
		wPamMap.put(CMT_TCH, wCmtTch);
		wPamMap.put(FRO_OAM, wCurUsr.getUsroam());
		wPamMap.put(FRO_USR, wCurUsr.getUsrnam());
		wPamMap.put(TOO_OAM, wTooOam);
		wPamMap.put(TOO_USR, wTooUsr);
		wPamMap.put(REP_CON, wRepCon);
		
		KSPDOM_ResDta wResDta = new KSPDOM_ResDta();
		try {
			wResDta = exeSrv(wPamMap);
		} catch (Exception e) {
			wResDta.setCod("400");
			wResDta.setMsg(e.getMessage());
		}
		return wResDta;
	}

	public KSPDOM_ResDta exeSrv(Map<String, Object> pPamMap) {
		repQue.add(pPamMap);
		if (!KSPR01Thd.repDealLock.isLocked()) {
			KSPR01Thd exeThd = new KSPR01Thd(repQue);
			repService.execute(exeThd);
		}

		return KSPDOM_ResDta.buildSucc("");
	}
	
}
